package com.example.artwindow.tool;

import cn.hutool.core.bean.BeanUtil;
import com.example.artwindow.domain.vo.CommentUserVO;
import com.example.artwindow.domain.vo.CommentVO;
import com.example.artwindow.result.ListResult;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CopyProperties{

	public static <T,U>List<T> copyProperties(List<U> uList,Class<T> tClass){
		return uList.stream().map(u->BeanUtil.copyProperties(u,tClass)).collect(Collectors.toList());
	}

	public static ListResult<CommentVO> setUser(List<CommentVO> commentVOS,List<CommentUserVO> list ,Long total,boolean subIs){
		Map<Long, CommentUserVO> collect = list.stream().collect(Collectors.toMap(CommentUserVO::getId, commentUserVO -> commentUserVO));
		if(subIs){
			commentVOS.forEach(commentVO -> {
				commentVO.setCommentUserVO(collect.get(commentVO.getCreateUser()));
				if(BeanUtil.isNotEmpty(collect.get(commentVO.getReplyUser())))
					commentVO.setReplayUserName(collect.get(commentVO.getReplyUser()).getName());

			});
		}else {
			commentVOS.forEach(commentVO -> {
				commentVO.setCommentUserVO(collect.get(commentVO.getCreateUser()));
			});
		}
		return new ListResult<>(total, commentVOS);
	}
}
